<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mexchange extends CI_Model{
    
    //获取奖项
    public function GetExchange(){
        $query = $this->db->query('select id,goods,coins from wtt_exchange order by id asc');
        return  $query->result_array();
    }

      public function GetExchangebyid($id){
        $query = $this->db->query('select id,goods,coins from wtt_exchange where id='.$id);
        return  $query->result_array();
    }

 
    
    public function EditExchange()
    {
       $arr_id = $this->input->post('id');
       $arr_goods = $this->input->post('goods');
       $arr_coins = $this->input->post('coins');
       
       $DB1 = $this->load->database('update', TRUE);
       foreach($arr_id as $key => $var)
       {
            $DB1->query("update wtt_exchange set goods='".$arr_goods[$key]."',coins=".$arr_coins[$key]." where id=".$var);
       }
       return true;
       
    }
    /*
    //获取奖池及状态
    public function GetExchanget()
    {
    	//$query = $this->db->query('select coins,sjifen,isopen from wtt_setp');
    	//return $query->row_array();
    }
    */
    //获取兑换列表
    function GetExchangeList($num)
    {
    	$query = $this->db->query('select wb_id,coins,goods,create_time from wtt_exchangeorder order by create_time desc limit 0,'.$num);
    	return $query->result_array();
    }
    /*
    //获取抽奖结果
    public function GetExchange($uid,$screen_name)
    {
       $DB1 = $this->load->database('update', TRUE);
        $arr_r = array(
        		'error_info' => '',
        		'info' => ''
        		);
        //及时获取用户币数
        $this->load->model('Mwbuser');
        //进行币数比较，少于30积分提醒币不足
        $uinfo = $this->Mwbuser->getinfo($uid,'coins');
        if($uinfo['coins'] < 5) 
        {
        	$arr_r['error_info'] = '您的币不足5，不能兑换！';
        	return $arr_r;
        }
        //获取实物列表
        $p_list = $this->GetGetExchange();
        //30要改成具体的实物要求的币数
        $this->Mwbuser->setcoins($uid,30,2);
        $this->Mwbuser->insertlog($uid,0,$screen_name,10,30,3,0);
        
        $arr_r['info']="兑换成功";
         
    	 
    	return $arr_r;
    }
      */
    //写入兑换列表
    public function insertlog($wb_id,$goodsid,$goods,$coins)
    {
    	$DB1 = $this->load->database('update', TRUE);
        $data = array(
    			'wb_id'=>$wb_id,
    			'goodsid' => $goodsid,    			
                               'goods' => $goods,
                               'coins' => $coins,
    			'create_time' => time()
    			);
         //echo $wb_id.'---'.$goodsid.'------'.$goods.'------'.$coins;
    	$DB1->insert('wtt_exchangeorder',$data);
    	$this->load->model('Mwbuser');
    	$uinfo = $this->Mwbuser->getinfo($wb_id);
    	$screen_name=$uinfo[screen_name];
       	$this->Mwbuser->insertlog($wb_id,0,$screen_name,10,0,$coins,2);
        return true;
    	//写入系统收入日志
    	 
    	//删除3天前的兑换纪录
    	//$deltime = time()-259200;
    	//$DB1->query('delete from wtt_exchangeorder where create_time<'.$deltime);
    }
  
     
}